package com.example.fitnessservice.service.impl;

import com.example.fitnessservice.module.dao.coachmapper.CoachMapper;
import com.example.fitnessservice.module.pojo.*;
import com.example.fitnessservice.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:XuRui
 * @Date:2022/11/21 22:12
 */
@Service
public class CoachServiceImpl implements CoachService {
    @Autowired
    private CoachMapper coachMapper;

    @Override
    public List<Coach> findAll(){
        List<Coach> coaches = coachMapper.findAll();
        return coaches;
    }
    //查询有空的教练
    @Override
    public List<Coach> findAll1(){
        //此处的字符串时间为从controller传过来，此时先写为死数据
        String str = "2022-11-25 15:00:00.000";
        List<Coach> coaches = null;
        if (str.equals("2022-11-25 10:00:00.000")) {
            coaches = coachMapper.findAll1();
        } else if (str.equals("2022-11-25 15:00:00.000")) {
            coaches = coachMapper.findAll2();
        } else {
            coaches = coachMapper.findAll3();
        }
        List<Coach> coaches1 = new ArrayList<>();
        for (Coach coach : coaches) {
            coach.setAge();
            if(coach.getId1()==null){
                coaches1.add(coach);
            }
        }
        return coaches1;
    }



    @Override
    public Integer findWalletByCid(Integer cid) {
        return coachMapper.findWalletByCid(cid);
    }

    @Override
    public List<CourseDetails> findClassByCid(Integer cid) {
        Coach coach = coachMapper.findClassByCid(cid);
        List<CourseDetails> courseDetails = coach.getCourseDetails();
        return courseDetails;
    }

    @Transactional
    @Override
    public List<CourseOrderDetails> findOldOrderByCid(Integer cid) {
        List<CourseOrderDetails> courseOrderDetails = coachMapper.findOrderByCid(cid);
        List<CourseOrderDetails> courseOrderDetails1 = new ArrayList<>();
        for (CourseOrderDetails courseOrderDetail : courseOrderDetails) {
            CourseDetails courseDetails = courseOrderDetail.getCourseDetails();
            Date date = courseDetails.getCourseEndTime();
            Date date1 = new Date();
            Integer orderStatus = courseOrderDetail.getOrderStatus();
            //订单状态2表示未开始，订单状态4表示进行中
            if (date1.after(date) && orderStatus == 4) {
                courseOrderDetail.setOrderStatus(1);
                courseDetails.setCourseStatus(1);
                coachMapper.updateCourse_statusById(courseDetails.getId());
                Integer id = courseOrderDetail.getId();
                coachMapper.updateorderStatus(id);
            }
            if (orderStatus == 1 || orderStatus == 3 || orderStatus == 6) {
                courseOrderDetails1.add(courseOrderDetail);
            }
        }
        return courseOrderDetails1;
    }
    @Transactional
    @Override
    public List<CourseOrderDetails> findNewOrderByCid(Integer cid) {
        List<CourseOrderDetails> courseOrderDetails = coachMapper.findOrderByCid(cid);
        List<CourseOrderDetails> courseOrderDetails1 = new ArrayList<>();
        for (CourseOrderDetails courseOrderDetail : courseOrderDetails) {
            CourseDetails courseDetails = courseOrderDetail.getCourseDetails();
            Date date = courseDetails.getCourseEndTime();
            Date date1 = new Date();
            Integer orderStatus = courseOrderDetail.getOrderStatus();
            if (date1.after(date) && orderStatus == 4) {
                courseOrderDetail.setOrderStatus(1);
                courseDetails.setCourseStatus(1);
                coachMapper.updateCourse_statusById(courseDetails.getId());
                Integer id = courseOrderDetail.getId();
                coachMapper.updateorderStatus(id);
            }
            if (orderStatus == 2 || orderStatus == 4) {
                courseOrderDetails1.add(courseOrderDetail);
            }
        }
        return courseOrderDetails1;
    }

    /***
     * 通过教练id查看我的通知中的预约申请
     * @param cid
     * @return
     */
    @Override
    public String findCourseOrderDetails(Integer cid) {
        List<CourseOrderDetails> courseOrderDetails = coachMapper.findCourseOrderDetails(cid);
        String str="";
        for (CourseOrderDetails courseOrderDetail : courseOrderDetails) {
            CourseDetails courseDetail = courseOrderDetail.getCourseDetails();
            if (courseDetail.getCourseStatus() == 1) {
                courseOrderDetail.setOrderStatus(1);
                coachMapper.updateorderStatus(courseOrderDetail.getId());
            }
            if (courseOrderDetail.getOrderStatus() == 1 || courseOrderDetail.getOrderStatus() == 2 || courseOrderDetail.getOrderStatus() == 4) {
                Integer uid = courseOrderDetail.getOrderUserId();
                UserInfo userInfo = coachMapper.findUserById(uid);
                Date date = courseOrderDetail.getOrderTime();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = sdf.format(date);
                str = "用户" + userInfo.getMonicker() + "于" + time + "预约了您的课程";
                coachMapper.addCoachBooking(cid, coachMapper.findNameByCid(cid), uid, userInfo.getMonicker(), time, courseOrderDetail.getOrderStatus(), courseOrderDetail.getOrderCourseId());
            }
        }
        return str;
    }

    /***
     * 只会展示订单还没完成的用户信息，这批人即为会员
     * @param cid
     * @return
     */
    @Override
    public List<CoachBooking> findCoachBookingByCid(Integer cid) {
        List<CoachBooking> coachBookingByCid = coachMapper.findCoachBookingByCid(cid);
        List<CoachBooking> coachBookings = new ArrayList<>();
        for (CoachBooking coachBooking : coachBookingByCid) {
            Integer courseId = coachBooking.getCourseId();
            CourseDetails courseDetail = coachMapper.findByCourseId(courseId);
            Date date = courseDetail.getCourseEndTime();
            Date date1 = new Date();
            if (date1.after(date) && courseDetail.getCourseStatus() == 4) {
                courseDetail.setCourseStatus(1);
                coachMapper.updateCourse_statusById(courseDetail.getId());
            }
            Integer courseStatus = courseDetail.getCourseStatus();
            if (courseStatus == 1) {
                coachBooking.setOrderStatus(1);
                coachMapper.updateBookingStatus(coachBooking.getId());
            }
            if (coachBooking.getOrderStatus() == 2 || coachBooking.getOrderStatus() == 4) {
                coachBookings.add(coachBooking);
            }
        }
        return coachBookings;
    }

    @Override
    public List<CourseDetails> findCourseDetails() {
        List<CourseDetails> courseDetails = coachMapper.findCourseDetails();
        return courseDetails;
    }


}
